import Vue from 'vue'
import config from "@/settings.js"

//	加载中...
Vue.prototype.$loading = function(title = '') {
	uni.showLoading({
		title
	});
}
//加载完成
Vue.prototype.$endLoading = function(title = '') {
	uni.hideLoading({
		title
	});
}
//	无图标提示
Vue.prototype.$toast = function(title) {
	uni.showToast({
		icon: 'none',
		title: title
	})
}

//	页面跳转
Vue.prototype.$nav = function(url) {
	uni.navigateTo({
		url,
		fail(){
			uni.switchTab({
				url
			})
		}
	})
}

//	关闭当前页面跳转
Vue.prototype.$redirect = function(url) {
	uni.redirectTo({
		url
	})
}

//	重启并去往页面
Vue.prototype.$relaunch = function(url) {
	uni.reLaunch({
		url
	})
}

// 上传图片
Vue.prototype.$uploadImage = function({
	success = null,
	fail = null,
	count = 1,
	sourceType=['album','camera']
}) {
	uni.chooseImage({
		count,
		sourceType,
		success: (res) => {
			uni.uploadFile({
				url: config.baseUrl + config.uploadUrl,
				filePath: res.tempFilePaths[0],
				name: 'file',
				header: {
					Token: uni.getStorageSync("token")
				},
				success: (res) => {
					// console.log("res: ",res);
					let data = JSON.parse(res.data)
					// console.log("data: ",data);
					// this.shortUrl = data.data.url;
					// this.uploadAvatar = data.data.fullurl;
					success && success(data.data)
				},
				fail: (err) => {
					this.$toast("上传失败")
					fail && fail(err)
				}
			})
		},
		fail(err) {
			fail && fail(err)
		}
	})
}